# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: Administrator
"""
#这个脚本时利用T1像做完segment后得到的结果，进一步的得到各种阈值（[0.4,0.5,0.6,0.7,0.8,0.9]）的灰质mask。

#%%
#将T1_SC_SS文件夹中的c1subj_001_T1_SC_SS.nii等复制到T1_SC_SS_GM文件夹，并且重新命名
import os 
import shutil
import glob
source_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS'
target_dirpath = source_dirpath + '_GM'
if not os.path.exists(target_dirpath):
    os.makedirs(target_dirpath)
suffix = '.nii'
partial_str = 'c1'
partial_str1 = 'GM'
star = '*'
filename_list = glob.glob(source_dirpath + os.sep + partial_str + star )
for source_filepath in filename_list:
    print('source_filepath:',source_filepath)
    target_filename = os.path.split(source_filepath)[1].replace(partial_str,'').replace('.nii',r'_GM.nii')
    target_filepath = target_dirpath + os.sep + target_filename
    shutil.copy2(source_filepath,target_filepath)

#%%
from agilepipe.spine import AgilePipe

#the code below is used to process T1
modality = '_T1'     #'_T1' '_BOLD' '_CBF'  '_PET'
manipulate_methods = { 'Spm_normalize_SN':{}
                      }
#dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
dirpath_dict = {'original_dirpath':[],
                     #用来存放其实的文件。
                     'Spm_normalize_SN':[]
                    }

#agile_pipe()用ccs处理BOLD的测试代码
original_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM'
#the first element of dirpath_list is very important which is used as deformation field 
#the second element of dirpath_list can be used in the reslice processing as the "Image Defining Space" filepath
dirpath_list = [r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS','/root/Documents/BrainMask_05_61x73x61.nii',''] #即使没有用也要赋值，否则会报错
dirpath_dict = dirpath_dict
manipulate_methods = manipulate_methods
parallel_core = 0.7
AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)

#%% 下面是分别得到[0.4,0.5,0.6,0.7,0.8,0.9]各个阈值的mask
from agilepipe.process import Limit_mask
original_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN'
limit = [0.4,0.5,0.6,0.7,0.8,0.9]
#limit = [0.4]
output_dirpath_list = []
for i in limit:
    output_dirpath_list.append(Limit_mask([original_dirpath,i]).output_dirpath)

#%%threhold at first,then reslice
#split the Yep template into 7 files.
from agilepipe.process.mask import Split_aaltemplate

source_filepath = r'/media/root/Elements4/XuanWu_dpabi_spm/mask/AAL_template/AAL_61x73x61_YCG.nii'
anatomy_index = 1
Split_aaltemplate([source_filepath,anatomy_index])

#%%
from agilepipe.spine import AgilePipe
blur_original_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN_LM-'
for i in range(4,10,1):
  #the code below is used to process T1
  modality = '_T1'     #'_T1' '_BOLD' '_CBF'  '_PET'
  manipulate_methods = { 
                        'Spm_reslice_SR':{},
                        'Equal_1_E1':{}
                        }
  #dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
  dirpath_dict = {'original_dirpath':[],
                       #用来存放其实的文件。
                       'Spm_reslice_SR':[],
                       'Equal_1_E1':[]
                      }
  
  #
  #the first element of dirpath_list is very important which is used as deformation field 
  #the second element of dirpath_list can be used in the reslice processing as the "Image Defining Space" filepath
  dirpath_list = [r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS','/root/Documents/BrainMask_05_61x73x61.nii',''] #即使没有用也要赋值，否则会报错
  dirpath_dict = dirpath_dict
  manipulate_methods = manipulate_methods
  parallel_core = 0.7
  original_dirpath = blur_original_dirpath + r'0.' + str(i)
  AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)
  
#%% 
from agilepipe.process.mask import  Equal_2_E2
for i in range(4,10):
  blur_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN'
  source1_dirpath = blur_dirpath + r'_LM-0.' + str(i) + r'_SR_E1'
  source2_dirpath = blur_dirpath + r'_SR_LM-0.' + str(i)
  output_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN_equal'
  print('source1_dirpath:',source1_dirpath)
  print('source2_dirpath:',source2_dirpath)
  Equal_2_E2([source1_dirpath,source2_dirpath,output_dirpath])

#%%

from agilepipe.process.mask import Add_2_A2
for i in range(4,10):
  blur_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN'
  source1_dirpath = blur_dirpath + r'_LM-0.' + str(i) + r'_SR_E1'
  source2_dirpath = blur_dirpath + r'_SR_LM-0.' + str(i)
  output_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/CBF_processing_ag/T1_SC_SS_GM_SN_add'
  print('source1_dirpath:',source1_dirpath)
  print('source2_dirpath:',source2_dirpath)
  Add_2_A2([source1_dirpath,source2_dirpath,output_dirpath])

#%%

from agilepipe.process.mask import Intersect_2mask
blur_mask1_ditpath = r'/media/root/Elements4/XuanWu_dpabi_spm/mask/T1_SC_SS_GM_SN_LM-'
mask1_dirpaths = [blur_mask1_ditpath +'0.'+ str(i) + '_SR_E1' for i in range(4,10)]
print('mask1_dirpaths:',mask1_dirpaths)
mask2_dirpath = r'/media/root/Elements4/XuanWu_dpabi_spm/mask/AAL_template/AAL_61x73x61_YCG_divided_mask'

for mask1_dirpath in mask1_dirpaths:
  Intersect_2mask([mask1_dirpath,mask2_dirpath])
